<template>
  <el-dialog
  title="题目预览"
  :visible="dialogVisible"
  width="60%"
  top='5vh'
  lock-scroll
  @closed='data={};isradio=false;ischecked=false;'
  @close='dialogVisible=false;'
  >
  <div v-if="!data.id"></div>
  <div v-else class="body" v-loading='loading'>
    <el-row>
      <el-col :span="6">【题型】:<span>{{$parent.type(data.questionType)}}</span></el-col>
      <el-col :span="6">【编号】:<span>{{data.id}}</span></el-col>
      <el-col :span="6">【难度】:<span>{{$parent.level(data.difficulty)}}</span></el-col>
      <el-col :span="6">【标签】:<span>{{data.tags}}</span></el-col>
    </el-row>
    <el-row>
      <el-col :span="6">【学科】:<span>{{data.subjectName}}</span></el-col>
      <el-col :span="6">【目录】:<span>{{data.directoryName}}</span></el-col>
      <el-col :span="6">【方向】:<span>{{data.direction}}</span></el-col>
    </el-row>
    <el-divider/>
    <el-col>【题干】:</el-col>
    <div>
      <h3>{{data.question|html2Text}}</h3>
      <div v-if="hasRight">
        <div>
          <span>{{istype}}</span> 选项:(以下选中的选项为正确答案)
        </div>
        <!-- 单选框 -->
        <div  v-if="isradio">
          <el-radio-group v-model="radio" @click.stop.prevent>
          <div v-for="item in data.options" class="clickbox" :key="item.id">
            <el-radio :label='item.isRight'>{{item.title}}</el-radio>
          </div>
          </el-radio-group>
        </div>
        <!-- 多选框 -->
        <div v-if="ischecked">
          <el-checkbox-group v-model="checklist" @change="handleCheckedCitiesChange" >
            <div :key='item.id' v-for="item in data.options" class="clickbox">
              <el-checkbox :v-model="checklist" :label="item.id">{{item.title}}</el-checkbox>
            </div>
          </el-checkbox-group>
        </div>
      </div>
    <el-divider/>
    <span>【参考答案】:<el-button type="danger" @click="isshow">视频答案预览</el-button></span>
    <el-divider/>
    <span>【答案解析】:<span>{{data.answer|html2Text}}</span></span>
    <el-divider/>
    <span>【题目备注】:<span>{{data.remarks}}</span></span>
    </div>
    <!-- 嵌套对话框 -->
    <el-dialog
      width="50%"
      :visible="innerVisible"
      append-to-body
      title="答案解析"
      @close='innerVisible=false;'>
      <div v-if="data.videoURL">
          <div class="player-container">
            <video-player class="vjs-custom-skin" :options="playerOptions">视频地址出错</video-player>
          </div>
      </div>
      <div v-else style='text-align:center;'>
        暂无
      </div>
    </el-dialog>
  </div>
  <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="dialogVisible = false;">关 闭</el-button>
  </span>
</el-dialog>
</template>

<script>
import { detail } from '@/api/hmmm/questions.js'
import 'video.js/dist/video-js.css'
import 'vue-video-player/src/custom-theme.css'
import { questionType } from '@/api/hmmm/constants'
export default {
  name: 'questionsdialog',
  data () {
    return {
      dialogVisible: false,
      radio: 1,
      data: {},
      loading: false,
      playerOptions: {},
      innerVisible: false,
      isradio: false,
      ischecked: false,
      checklist: []
    }
  },
  created () {
  },
  methods: {
    async getinfo (value) { // 数据渲染
      this.loading = true
      try {
        const { data } = await detail(value)
        console.log(data.options)
        this.data = data
        this.loading = false
      } catch (error) {
        this.loading = false
      }
    },
    isshow () { // 显示视频 MediaError
      if (!this.data.videoURL.includes(['http'])) this.data.videoURL = ''
      this.playerOptions = {
        playbackRates: [0.7, 1.0, 1.5, 2.0], // 播放速度
        autoplay: false, // 如果true,浏览器准备好时开始回放。
        controls: true, // 控制条
        preload: 'auto', // 视频预加载
        muted: false, // 默认情况下将会消除任何音频。
        loop: false, // 导致视频一结束就重新开始。
        language: 'zh-CN',
        aspectRatio: '16:9', // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
        fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
        sources: [{
          type: 'video/mp4',
          src: this.data.videoURL
          // src: 'src/views/sjsc/sjsc01_carInfoManage/2.mp4'//你所放置的视频的地址，最好是放在服务器上
        }],
        // poster: "http://39.106.117.192:8080/static/indexImg.png", //你的封面地址（覆盖在视频上面的图片）
        // width: document.documentElement.clientWidth,
        notSupportedMessage: '此视频暂无法播放，请稍后再试' // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
      }

      console.log(this.data.videoURL)
      this.innerVisible = true
    },
    type () { // 问题类型
      if (this.istype === '单选') this.isradio = true
      else if (this.istype === '多选') {
        this.ischecked = true
        const arr = this.data.options.filter(v => v.isRight === 1)
        this.checklist = arr.map(v => v.id)
        console.log(this.checklist)
      }
    }
  },
  computed: {
    istype () { // 问题类型
      const obj = questionType.find(v => v.value === +this.data.questionType)
      if (!obj) return null
      return obj.label
    },
    hasRight () { // 展示下面
      return this.data.options.some(v => v.isRight === 1)
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body{
  .body{
    font-size: 16px;
  }
 h3{
   margin-top: 60px;
   color: blue;
   font-weight: 400;
 }
 ::v-deep.el-divider--horizontal{
   margin: 10px 0;
 }
 .clickbox{
   margin:20px 0;
 }
}
::v-deep.el-col{
  margin: 10px 0;
  overflow: hidden;
  white-space:nowrap;
  text-overflow:ellipsis;
}
</style>
